package com.funning.action;


import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.funning.bean.OfFuning;
import com.funning.biz.IFuning;
import com.funning.dao.IFuningDAO;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 笑话管理的action
 * @author meishuangyan
 *
 */
@SuppressWarnings("serial")
public class funingAction extends ActionSupport {
	
	private IFuning funing;
	private OfFuning ofFuning;
	private IFuningDAO funingDao;

	/**
	 * 添加笑话
	 * @return
	 */
	public String AddFuning() {
		String reslut="";
		ofFuning=new OfFuning();
		String ofDesc=ServletActionContext.getRequest().getParameter("of_desc");
		if(ofDesc == null || ofDesc.equals("")){
			ServletActionContext.getRequest().setAttribute("msg","输入的笑话内容不能为空！！！！请重新输入");
			reslut = "error";
		}else if(ofDesc.length()>1000){
			ServletActionContext.getRequest().setAttribute("msg","输入的笑话内容不能大于1000个字");
			reslut = "error";
		}else{
			ofDesc.trim();
			ofFuning.setOfDesc(ofDesc);
			Date  date = new Date();
			System.out.println(date);
			ofFuning.setOfTime(date);
			if(funing.insertFuning(ofFuning)){
				reslut = "success";
			}
		}
		
		return reslut;
	}
	
	/**
	 * 查看所有的笑话
	 * @return
	 */
	public String getAllFuning(){
		String reslut = "";
		List<OfFuning> list=getFuning().getAllFuning();
		if(list == null || list.size()<=0){
			ServletActionContext.getRequest().setAttribute("msg","查询的笑话内容为空！请重新查询..");
			reslut = "error";
		}else{
			ServletActionContext.getRequest().setAttribute("funningList",list);
			reslut = "success";
		}
		return reslut;
	}
	
	
	/**
	 * 查询出最新的笑话
	 * @return
	 */
	public String getNewFuning() {
		String reslut="success";
		OfFuning	ofuning=funing.getNewFuning();
		if(ofuning!=null ){
			ServletActionContext.getRequest().setAttribute("NewofFuning", ofuning);
			reslut = "success";
		}
		return reslut;
	}
	/**
	 * 查询出前三条的笑话
	 */
	public String getThreeFuning(){
		String reslut = "success";
		List<OfFuning> ofuning = funing.getThreeFuning();
		if(ofuning!=null && ofuning.size()>0){
			ServletActionContext.getRequest().setAttribute("NewofFuning", ofuning);
			reslut = "success";
		}
		return reslut;
	}
	
	
	/**
	 *通过id查询笑话
	 * @return
	 */
	public String getFuningById() {
		String reslut = "";
		String id=ServletActionContext.getRequest().getParameter("id");
		if(id == null){
			ServletActionContext.getRequest().setAttribute("msg","笑话ID不能为空！");
			reslut = "error";
		}else{
			long ofId=Long.parseLong(id);
			
			ofFuning=funing.getFuningById(ofId);
			if(ofFuning!=null){
				ServletActionContext.getRequest().setAttribute("ofFuning", ofFuning);
				reslut = "success";
				}
		}
		
		return reslut;
	}
	
	/**
	 * 更新笑话
	 * @return
	 */
	public String updateFuning() {
		int reslut=-1;
		String re = "";
		String id=ServletActionContext.getRequest().getParameter("ofId");
		String ofDesc=ServletActionContext.getRequest().getParameter("ofDesc");
		if(id == null || ofDesc == null || ofDesc.equals("")){
			ServletActionContext.getRequest().setAttribute("msg","输入的笑话内容不能为空！！！！请重新输入");
			re = "error";
		}else{
			long ofId=Long.parseLong(id);
			
			Date date=new Date();
			ofFuning=new OfFuning();
			ofFuning.setOfId(ofId);
			ofFuning.setOfDesc(ofDesc);
			ofFuning.setOfTime(date);
			reslut=funing.updateFuning(ofFuning);
			if(reslut>0){
				ServletActionContext.getRequest().setAttribute("ofFuning", ofFuning);
				re = "success";
			}
		}
		
		return re;
	}
	/**
	 * 删除笑话
	 * @return
	 */
	public String delFuning(){
		int reslut=-1;
		String re = "error";
		String id=ServletActionContext.getRequest().getParameter("id");
		long ofId=Long.parseLong(id);
		reslut=funing.deleteFuning(ofId);
		if(reslut>0){
			re = "success";
		}
		return re;
	}


	
	
	public IFuningDAO getFuningDao() {
		return funingDao;
	}


	public void setFuningDao(IFuningDAO funingDao) {
		this.funingDao = funingDao;
	}


	public IFuning getFuning() {
		return funing;
	}

	public void setFuning(IFuning funing) {
		this.funing = funing;
	}

	public OfFuning getOfFuning() {
		return ofFuning;
	}

	public void setOfFuning(OfFuning ofFuning) {
		this.ofFuning = ofFuning;
	}

	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	

}
